import json
from py2neo import Graph, Node, Relationship
from py2neo.matching import *

names = []  # 病名
categorys = []  # 所属病种
symptoms = []  # 症状
acompanys = []  # 并发症
cure_departments = []  # 挂号、治疗科室
cure_ways = []  # 治疗方法
checks = []  # 检查方法
do_eats = []  # 要吃的食物
not_eats = []  # 不能吃的食物
recommand_eats = []  # 推荐吃的东西
recommand_drugs = []  # 推荐吃的药
drug_details = []  # 治疗药物的细节



with open("./data/medical2.json", 'r', encoding='GBK') as f:
    for line in f:
        data = json.loads(line)
        print("name:", data['name'])
        print('category:', data['category'])
        print('symptom', data['symptom'])
        print('acompany', data['acompany'])
        print('cure_department', data['cure_department'])
        print('cure_way', data['cure_way'])
        print('check', data['check'])

        names.append(data['name'])
        categorys.extend(data['category'])
        symptoms.extend(data['symptom'])
        acompanys.extend(data['acompany'])
        cure_departments.extend(data['cure_department'])
        cure_ways.extend(data['cure_way'])
        checks.extend(data['check'])

        try:
            print('do_eat', data['do_eat'])
            do_eats.extend(data['do_eat'])
        except KeyError:
            print("没有do_eat")

        try:
            print('not_eat', data['not_eat'])
            not_eats.extend(data['not_eat'])
        except KeyError:
            print("没有not_eat")

        try:
            print('recommand_eat', data['recommand_eat'])
            recommand_eats.extend(data['recommand_eat'])
        except KeyError:
            print("没有recommand_eat")

        try:
            print('recommand_drug', data['recommand_drug'])
            recommand_drugs.extend(data['recommand_drug'])
        except KeyError:
            print("没有recommand_drug")

        try:
            print('drug_detail', data['drug_detail'])
            drug_details.extend(data['drug_detail'])
        except KeyError:
            print("没有drug_detail")
        print("===============================================================================================================================")

"""去重"""
names = list(set(names))
categorys = list(set(categorys))
symptoms = list(set(symptoms))
acompanys = list(set(acompanys))
cure_departments = list(set(cure_departments))
cure_ways = list(set(cure_ways))
checks = list(set(checks))
do_eats = list(set(do_eats))
not_eats = list(set(not_eats))
recommand_eats = list(set(recommand_eats))
recommand_drugs = list(set(recommand_drugs))
drug_details = list(set(drug_details))

graph = Graph("http://localhost:7474/", auth=("neo4j", "123456"))

# 删除数据库中原有的节点
graph.run("match (n) detach delete n")
print("数据库删除成功！")

"""在数据库建立节点"""
for name in names:
    name_node = Node("name", name=name)
    graph.create(name_node)

for category in categorys:
    category_node = Node("category", name=category)
    graph.create(category_node)

for symptom in symptoms:
    symptom_node = Node("symptoms", name=symptom)
    graph.create(symptom_node)

for acompany in acompanys:
    acompany_node = Node("acompany", name=acompany)
    graph.create(acompany_node)

for cure_department in cure_departments:
    cure_department_node = Node("cure_department", name=cure_department)
    graph.create(cure_department_node)

for cure_way in cure_ways:
    cure_way_node = Node("cure_way", name=cure_way)
    graph.create(cure_way_node)

for check in checks:
    check_node = Node("check", name=check)
    graph.create(check_node)

for do_eat in do_eats:
    do_eat_node = Node("do_eat", name=do_eat)
    graph.create(do_eat_node)

for not_eat in not_eats:
    not_eat_node = Node("not_eat", name=not_eat)
    graph.create(not_eat_node)

for recommand_eat in recommand_eats:
    recommand_eat_node = Node("recommand_eat", name=recommand_eat)
    graph.create(recommand_eat_node)

for recommand_drug in recommand_drugs:
    recommand_drug_node = Node("recommand_drug", name=recommand_drug)
    graph.create(recommand_drug_node)

for drug_detail in drug_details:
    drug_detail_node = Node("drug_detail", name=drug_detail)
    graph.create(drug_detail_node)


"""构建关系指向"""
attributes = ['category', 'symptom', 'acompany', 'cure_department', 'cure_way', 'check', 'do_eat', 'not_eat', 'recommand_eat', 'recommand_drug', 'drug_detail']
nodes = NodeMatcher(graph)
with open("./data/medical2.json", 'r', encoding='GBK') as f:
    for line in f:
        data = json.loads(line)
        for attribute in attributes:
            for c in data['category']:
                print(c)
                print(data['name'])
                node1 = nodes.match("name", name=data['name']).first()
                node2 = nodes.match("category", name=c).first()
                re = Relationship(node1, 'name_category', node2)
                graph.create(re)
            print("关系创建成功！")








